import re
        
def format_json_response(response):
    """
    Format and clean up JSON responses from LLMs that might not be properly formatted.
    Handles thinking model output format with <think>...</think> sections.
    
    Args:
        response (str): The raw response from the LLM
        
    Returns:
        str: A properly formatted JSON string
    """
    # Remove thinking process if present
    think_pattern = r'<think>.*?</think>\s*'  # Non-greedy match for thinking process
    response = re.sub(think_pattern, '', response, flags=re.DOTALL)
    
    # Remove markdown code block indicators
    response = re.sub(r'```json|```', '', response)
    
    # Trim whitespace
    response = response.strip()
    
    # Replace any double newlines or excessive whitespace
    response = re.sub(r'\n\s*\n', '\n', response)
    
    return response
